package day3;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

//模拟一个用户系统：序列化实战功能
//1.查询用户
//2.新增用户
//3.删除用户
//4.修改用户
//5.退出系统
//并且 程序关闭了 再启动还可以读取到之前存储的数据(数据库 序列化)
public class UserSystem {
    //保存所有注册的用户数据
    static List<User> users = new ArrayList<>();
    static Scanner sc = new Scanner(System.in);

    static {//类加载执行 而且执行一次
        //刚刚启动程序 需要做反序列化 读取集合数据
        input();
    }

    //刚刚启动程序，需要做反序列化，读写集合数据
    public static void main(String[] args) {
        menu();
        System.out.println("系统结束了");
    }

    public static void menu() {//显示菜单的方法
        while (true) {
            System.out.println("邓志涛疯狂学习用户系统");
            System.out.println("1.查询用户");
            System.out.println("2.新增用户");
            System.out.println("3.删除用户");
            System.out.println("4.修改用户");
            System.out.println("5.退出系统");
            System.out.println("********");
            System.out.println("请输入你的选择：");
            int choose = sc.nextInt();
            switch (choose) {
                case 1:
                    show();
                    break;
                case 2:
                    add();
                    break;
                case 3:
                    delete();
                    break;
                case 4:
                    update();
                    break;
                case 5:
                    return;//结束这个方法
            }
        }
    }

    public static void show() {
        System.out.println("编号\t账号\t密码\t城市");
        for (int i = 0; i < users.size(); i++) {
            User u = users.get(i);
            System.out.println((i + 1) + "\t" + u.getUsername() + "\t" + u.getPassword() + "\t" + u.getCity());
        }
    }

    public static void add() {
        System.out.println("输入账号");
        String username = sc.next();
        System.out.println("输入密码");
        String password = sc.next();
        System.out.println("输入城市");
        String city = sc.next();
        users.add(new User(username, password, city));
        System.out.println(username + "添加成功");
        //数据修改了，就需要重新保存新数据，序列化
        output();
    }

    public static void delete() {
        //数据修改了，就需要重新保存新数据，序列化
        output();
    }

    public static void update() {
        //数据修改了，就需要重新保存新数据，序列化
        output();
    }

    //序列化
    public static void output() {
        try (FileOutputStream fos = new FileOutputStream("d://io//Users.bb");
             ObjectOutputStream oos = new ObjectOutputStream(fos)) {
            oos.writeObject(users);
            System.out.println("集合更新成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //反序列化
    public static void input() {
        try (FileInputStream fis = new FileInputStream("d://io//Users.bb");
             ObjectInputStream ois = new ObjectInputStream(fis)) {
            users = (List<User>) ois.readObject();
            System.out.println("集合加载成功");
        } catch (FileNotFoundException e) {
            System.out.println("欢迎第一次使用");
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

class User implements Serializable {
    private String username;
    private String password;
    private String city;

    public User(String username, String password, String city) {
        this.username = username;
        this.password = password;
        this.city = city;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }
}
